{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Running ProjectQ code on IBM Q devices"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this tutorial, we will see how to run code on IBM Q devices directly from within ProjectQ. All that is needed is an IBM Q Experience user account. To sign up, visit https://quantumexperience.ng.bluemix.net/.\n",
    "\n",
    "ProjectQ supports two IBM Q devices called `ibmqx4` and `ibmqx5` which feature 5 and 16 qubits, respectively. Let us start with entangling the qubits of the 5-qubit device:\n",
    "\n",
    "## Entangling 5 qubits\n",
    "First, we import all necessary operations (`Entangle`, measurement), the back-end (`IBMBackend`), and the main compiler engine (`MainEngine`). The Entangle operation is defined as a Hadamard gate on the first qubit (creates an equal superposition of |0> and |1>), followed by controlled NOT gates acting on all other qubits controlled on the first."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import projectq.setups.ibm\n",
    "from projectq.backends import IBMBackend\n",
    "from projectq.ops import Measure, Entangle, All\n",
    "from projectq import MainEngine"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we instantiate a main compiler engine using the IBM Q back-end and the predefined compiler engines which take care of the qubit placement, translation of operations, etc.:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "eng = MainEngine(IBMBackend(use_hardware=True, num_runs=1024,\n",
    "                            verbose=False, device='ibmqx4'),\n",
    "                 engine_list=projectq.setups.ibm.get_engine_list())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If `use_hardware` is set to `False`, it will use the IBM Q simulator instead. `num_runs` specifies the number of samples to collect for statistics, `verbose=True` would output additional information which may be helpful for debugging, and the device parameter lets users choose between the two devices (\"ibmqx4\" and \"ibmqx5\").\n",
    "\n",
    "With our compiler set up, we can now allocate our qubits, entangle them, measure the outcome, and then flush the entire circuit down the compilation pipeline such that it is executed (and measurements are registered). Note that there are many jobs queued for execution on the IBM Q device and, as a result, our execution times out. We will learn how to retrieve our results despite this time out."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "IBM QE user (e-mail) > haenert@phys.ethz.ch\n",
      "IBM QE password > \n",
      "Waiting for results. [Job ID: 5b557df2306393003b746da2]\n",
      "Currently there are 49 jobs queued for execution on ibmqx4.\n",
      "Currently there are 48 jobs queued for execution on ibmqx4.\n"
     ]
    },
    {
     "ename": "Exception",
     "evalue": "Timeout. The ID of your submitted job is 5b557df2306393003b746da2.\n raised in:\n'  File \"/home/thomas/ProjectQ/projectq/backends/_ibm/_ibm_http_client.py\", line 174, in _get_result'\n'    .format(execution_id))'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mException\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-4dfb665c4ebe>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     20\u001b[0m     \u001b[0;32mreturn\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mq\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mq\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mqureg\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     21\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 22\u001b[0;31m \u001b[0mrun_entangle\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0meng\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnum_qubits\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m  \u001b[0;31m# run it\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m<ipython-input-3-4dfb665c4ebe>\u001b[0m in \u001b[0;36mrun_entangle\u001b[0;34m(eng, num_qubits)\u001b[0m\n\u001b[1;32m     10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     11\u001b[0m     \u001b[0;31m# run the circuit\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m     \u001b[0meng\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mflush\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     14\u001b[0m     \u001b[0;31m# access the probabilities via the back-end:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/home/thomas/ProjectQ/projectq/cengines/_main.py\u001b[0m in \u001b[0;36mflush\u001b[0;34m(self, deallocate_qubits)\u001b[0m\n\u001b[1;32m    302\u001b[0m                 \u001b[0mqb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mactive_qubits\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    303\u001b[0m                 \u001b[0mqb\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__del__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 304\u001b[0;31m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreceive\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mCommand\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mFlushGate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mWeakQubitRef\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/home/thomas/ProjectQ/projectq/cengines/_main.py\u001b[0m in \u001b[0;36mreceive\u001b[0;34m(self, command_list)\u001b[0m\n\u001b[1;32m    264\u001b[0m                 then send on)\n\u001b[1;32m    265\u001b[0m         \"\"\"\n\u001b[0;32m--> 266\u001b[0;31m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcommand_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    267\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    268\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0msend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcommand_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/home/thomas/ProjectQ/projectq/cengines/_main.py\u001b[0m in \u001b[0;36msend\u001b[0;34m(self, command_list)\u001b[0m\n\u001b[1;32m    286\u001b[0m                                              \"\\n\" + repr(last_line[-2]))\n\u001b[1;32m    287\u001b[0m                 \u001b[0mcompact_exception\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__cause__\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 288\u001b[0;31m                 \u001b[0;32mraise\u001b[0m \u001b[0mcompact_exception\u001b[0m  \u001b[0;31m# use verbose=True for more info\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    289\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    290\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mflush\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdeallocate_qubits\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mException\u001b[0m: Timeout. The ID of your submitted job is 5b557df2306393003b746da2.\n raised in:\n'  File \"/home/thomas/ProjectQ/projectq/backends/_ibm/_ibm_http_client.py\", line 174, in _get_result'\n'    .format(execution_id))'"
     ]
    }
   ],
   "source": [
    "def run_entangle(eng, num_qubits):\n",
    "    # allocate a quantum register of 5 qubits\n",
    "    qureg = eng.allocate_qureg(num_qubits)\n",
    "\n",
    "    # entangle the qureg\n",
    "    Entangle | qureg\n",
    "\n",
    "    # measure; should be all-0 or all-1\n",
    "    All(Measure) | qureg\n",
    "\n",
    "    # run the circuit\n",
    "    eng.flush()\n",
    "\n",
    "    # access the probabilities via the back-end:\n",
    "    results = eng.backend.get_probabilities(qureg)\n",
    "    for state in results:\n",
    "        print(\"Measured {} with p = {}.\".format(state, results[state]))\n",
    "\n",
    "    # return one (random) measurement outcome.\n",
    "    return [int(q) for q in qureg]\n",
    "\n",
    "run_entangle(eng, num_qubits=5)  # run it"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Retrieving a timed-out execution\n",
    "Sometimes, the queue is very long and the waiting times may exceed the limit of 5 minutes. In this case, ProjectQ will raise an exception which contains the job ID, as could be seen above, where the job ID was `5b557df2306393003b746da2`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In order to still retrieve all results at a later point in time, one can simply re-run the entire program using a slightly modified back-end:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "IBM QE user (e-mail) > haenert@phys.ethz.ch\n",
      "IBM QE password > \n",
      "Waiting for results. [Job ID: 5b557df2306393003b746da2]\n",
      "Measured 00001 with p = 0.0185546875.\n",
      "Measured 01101 with p = 0.00390625.\n",
      "Measured 10001 with p = 0.0107421875.\n",
      "Measured 11001 with p = 0.0029296875.\n",
      "Measured 10101 with p = 0.0107421875.\n",
      "Measured 11101 with p = 0.0419921875.\n",
      "Measured 00011 with p = 0.005859375.\n",
      "Measured 01011 with p = 0.00390625.\n",
      "Measured 00111 with p = 0.0029296875.\n",
      "Measured 01111 with p = 0.0107421875.\n",
      "Measured 10011 with p = 0.0322265625.\n",
      "Measured 11011 with p = 0.0419921875.\n",
      "Measured 10111 with p = 0.056640625.\n",
      "Measured 11111 with p = 0.2744140625.\n",
      "Measured 00000 with p = 0.392578125.\n",
      "Measured 01000 with p = 0.0029296875.\n",
      "Measured 00100 with p = 0.01171875.\n",
      "Measured 01100 with p = 0.0126953125.\n",
      "Measured 10000 with p = 0.0009765625.\n",
      "Measured 00010 with p = 0.009765625.\n",
      "Measured 01010 with p = 0.0009765625.\n",
      "Measured 00110 with p = 0.0029296875.\n",
      "Measured 01110 with p = 0.0087890625.\n",
      "Measured 10010 with p = 0.0029296875.\n",
      "Measured 11010 with p = 0.0068359375.\n",
      "Measured 10110 with p = 0.00390625.\n",
      "Measured 11110 with p = 0.025390625.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0, 0, 0, 0, 0]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eng = MainEngine(IBMBackend(use_hardware=True, num_runs=1024,\n",
    "                            verbose=False, device='ibmqx4',\n",
    "                            retrieve_execution=\"5b557df2306393003b746da2\"),  # provide job ID\n",
    "                 engine_list=projectq.setups.ibm.get_engine_list())\n",
    "\n",
    "run_entangle(eng, num_qubits=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Entangling more qubits: Using ibmqx5\n",
    "\n",
    "If you have access to the 16-qubit device as well, you can also use ProjectQ to run your quantum programs on this device. ProjectQ contains a 2D grid mapper, which takes care of the mapping for you. We only have to change two things in order to use the 16-qubit chip as opposed to the 5-qubit chip:\n",
    "\n",
    "1) Import the new 16-qubit setup which contains the compiler engines for this device\n",
    "\n",
    "2) Modify the device parameter in the IBMBackend to \"ibmqx5\"\n",
    "\n",
    "Therefore, in order to entangle more than 5 qubits, we can simply write"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import projectq.setups.ibm16  # import setup which contains the grid mapper\n",
    "eng = MainEngine(IBMBackend(use_hardware=True, num_runs=1024,\n",
    "                            verbose=False, device='ibmqx5'),  # use ibmqx5 now\n",
    "                 engine_list=projectq.setups.ibm16.get_engine_list())  # note: ibm16 setup"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and then re-run the example from before via `run_entangle(eng, num_qubits)`. If an execution times out, it can also be retrieved at a later point by providing the additional `retrieve_execution=\"execution_id\"` parameter to the IBMBackend (but this time with `device='ibmqx5'`)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "IBM QE user (e-mail) > haenert@phys.ethz.ch\n",
      "IBM QE password > \n",
      "Waiting for results. [Job ID: 5b5580e0e291fd003ea62acf]\n",
      "Currently there are 12 jobs queued for execution on ibmqx5.\n",
      "Currently there are 12 jobs queued for execution on ibmqx5.\n",
      "Measured 00000000 with p = 0.0234375.\n",
      "Measured 00100000 with p = 0.017578125.\n",
      "Measured 01000000 with p = 0.0234375.\n",
      "Measured 01100000 with p = 0.0087890625.\n",
      "Measured 00010000 with p = 0.013671875.\n",
      "Measured 00110000 with p = 0.0126953125.\n",
      "Measured 01010000 with p = 0.0146484375.\n",
      "Measured 01110000 with p = 0.013671875.\n",
      "Measured 00000010 with p = 0.013671875.\n",
      "Measured 00100010 with p = 0.009765625.\n",
      "Measured 01000010 with p = 0.0107421875.\n",
      "Measured 01100010 with p = 0.0068359375.\n",
      "Measured 00010010 with p = 0.0048828125.\n",
      "Measured 00110010 with p = 0.0078125.\n",
      "Measured 01010010 with p = 0.0068359375.\n",
      "Measured 01110010 with p = 0.0078125.\n",
      "Measured 00000100 with p = 0.001953125.\n",
      "Measured 00100100 with p = 0.009765625.\n",
      "Measured 01000100 with p = 0.0068359375.\n",
      "Measured 01100100 with p = 0.0048828125.\n",
      "Measured 00010100 with p = 0.005859375.\n",
      "Measured 00110100 with p = 0.005859375.\n",
      "Measured 01010100 with p = 0.001953125.\n",
      "Measured 01110100 with p = 0.005859375.\n",
      "Measured 00000110 with p = 0.001953125.\n",
      "Measured 00100110 with p = 0.005859375.\n",
      "Measured 01000110 with p = 0.00390625.\n",
      "Measured 01100110 with p = 0.005859375.\n",
      "Measured 00010110 with p = 0.0107421875.\n",
      "Measured 00110110 with p = 0.0009765625.\n",
      "Measured 01010110 with p = 0.001953125.\n",
      "Measured 01110110 with p = 0.0029296875.\n",
      "Measured 10000000 with p = 0.0009765625.\n",
      "Measured 10100000 with p = 0.0087890625.\n",
      "Measured 11000000 with p = 0.001953125.\n",
      "Measured 11100000 with p = 0.0029296875.\n",
      "Measured 10010000 with p = 0.0029296875.\n",
      "Measured 10110000 with p = 0.001953125.\n",
      "Measured 11010000 with p = 0.001953125.\n",
      "Measured 11110000 with p = 0.0009765625.\n",
      "Measured 10000010 with p = 0.0009765625.\n",
      "Measured 10100010 with p = 0.0048828125.\n",
      "Measured 11000010 with p = 0.0009765625.\n",
      "Measured 11100010 with p = 0.0029296875.\n",
      "Measured 10010010 with p = 0.001953125.\n",
      "Measured 10110010 with p = 0.0087890625.\n",
      "Measured 11010010 with p = 0.0009765625.\n",
      "Measured 11110010 with p = 0.0009765625.\n",
      "Measured 10000100 with p = 0.0009765625.\n",
      "Measured 10100100 with p = 0.0009765625.\n",
      "Measured 11000100 with p = 0.0048828125.\n",
      "Measured 10010100 with p = 0.0048828125.\n",
      "Measured 10110100 with p = 0.001953125.\n",
      "Measured 11010100 with p = 0.0029296875.\n",
      "Measured 11110100 with p = 0.001953125.\n",
      "Measured 10000110 with p = 0.001953125.\n",
      "Measured 10100110 with p = 0.001953125.\n",
      "Measured 11100110 with p = 0.0029296875.\n",
      "Measured 10010110 with p = 0.001953125.\n",
      "Measured 10110110 with p = 0.001953125.\n",
      "Measured 11110110 with p = 0.0048828125.\n",
      "Measured 00000001 with p = 0.0029296875.\n",
      "Measured 00100001 with p = 0.0029296875.\n",
      "Measured 01000001 with p = 0.0029296875.\n",
      "Measured 01100001 with p = 0.0009765625.\n",
      "Measured 00110001 with p = 0.00390625.\n",
      "Measured 01010001 with p = 0.005859375.\n",
      "Measured 01110001 with p = 0.0009765625.\n",
      "Measured 00000011 with p = 0.0029296875.\n",
      "Measured 00100011 with p = 0.0029296875.\n",
      "Measured 01000011 with p = 0.0009765625.\n",
      "Measured 01100011 with p = 0.0009765625.\n",
      "Measured 00010011 with p = 0.0029296875.\n",
      "Measured 00000101 with p = 0.0029296875.\n",
      "Measured 00100101 with p = 0.0029296875.\n",
      "Measured 01000101 with p = 0.001953125.\n",
      "Measured 01100101 with p = 0.0029296875.\n",
      "Measured 00110101 with p = 0.001953125.\n",
      "Measured 01010101 with p = 0.001953125.\n",
      "Measured 01110101 with p = 0.0029296875.\n",
      "Measured 00000111 with p = 0.0029296875.\n",
      "Measured 01100111 with p = 0.0009765625.\n",
      "Measured 00010111 with p = 0.001953125.\n",
      "Measured 00110111 with p = 0.0009765625.\n",
      "Measured 01010111 with p = 0.0009765625.\n",
      "Measured 01110111 with p = 0.001953125.\n",
      "Measured 10000001 with p = 0.00390625.\n",
      "Measured 10100001 with p = 0.001953125.\n",
      "Measured 11000001 with p = 0.0029296875.\n",
      "Measured 11100001 with p = 0.0048828125.\n",
      "Measured 10010001 with p = 0.0048828125.\n",
      "Measured 10110001 with p = 0.0029296875.\n",
      "Measured 11010001 with p = 0.001953125.\n",
      "Measured 11110001 with p = 0.0029296875.\n",
      "Measured 10000011 with p = 0.0029296875.\n",
      "Measured 10100011 with p = 0.0048828125.\n",
      "Measured 11000011 with p = 0.0048828125.\n",
      "Measured 11100011 with p = 0.0029296875.\n",
      "Measured 10010011 with p = 0.001953125.\n",
      "Measured 10110011 with p = 0.001953125.\n",
      "Measured 11010011 with p = 0.001953125.\n",
      "Measured 11110011 with p = 0.0029296875.\n",
      "Measured 10000101 with p = 0.005859375.\n",
      "Measured 10100101 with p = 0.0107421875.\n",
      "Measured 11000101 with p = 0.009765625.\n",
      "Measured 11100101 with p = 0.0029296875.\n",
      "Measured 10010101 with p = 0.0078125.\n",
      "Measured 10110101 with p = 0.0068359375.\n",
      "Measured 11010101 with p = 0.0078125.\n",
      "Measured 11110101 with p = 0.00390625.\n",
      "Measured 10000111 with p = 0.0078125.\n",
      "Measured 10100111 with p = 0.005859375.\n",
      "Measured 11000111 with p = 0.001953125.\n",
      "Measured 11100111 with p = 0.0048828125.\n",
      "Measured 10010111 with p = 0.0048828125.\n",
      "Measured 10110111 with p = 0.001953125.\n",
      "Measured 11010111 with p = 0.00390625.\n",
      "Measured 11110111 with p = 0.0068359375.\n",
      "Measured 00001000 with p = 0.0087890625.\n",
      "Measured 00101000 with p = 0.017578125.\n",
      "Measured 01001000 with p = 0.0107421875.\n",
      "Measured 01101000 with p = 0.0146484375.\n",
      "Measured 00011000 with p = 0.0048828125.\n",
      "Measured 00111000 with p = 0.01171875.\n",
      "Measured 01011000 with p = 0.0126953125.\n",
      "Measured 01111000 with p = 0.0146484375.\n",
      "Measured 00001010 with p = 0.009765625.\n",
      "Measured 00101010 with p = 0.005859375.\n",
      "Measured 01001010 with p = 0.0029296875.\n",
      "Measured 01101010 with p = 0.017578125.\n",
      "Measured 00011010 with p = 0.0087890625.\n",
      "Measured 00111010 with p = 0.01171875.\n",
      "Measured 01011010 with p = 0.0029296875.\n",
      "Measured 01111010 with p = 0.00390625.\n",
      "Measured 00001100 with p = 0.0068359375.\n",
      "Measured 00101100 with p = 0.001953125.\n",
      "Measured 01001100 with p = 0.005859375.\n",
      "Measured 01101100 with p = 0.0078125.\n",
      "Measured 00011100 with p = 0.005859375.\n",
      "Measured 00111100 with p = 0.00390625.\n",
      "Measured 01011100 with p = 0.00390625.\n",
      "Measured 01111100 with p = 0.0068359375.\n",
      "Measured 00001110 with p = 0.0029296875.\n",
      "Measured 00101110 with p = 0.00390625.\n",
      "Measured 01101110 with p = 0.0029296875.\n",
      "Measured 00011110 with p = 0.0048828125.\n",
      "Measured 00111110 with p = 0.00390625.\n",
      "Measured 01011110 with p = 0.001953125.\n",
      "Measured 01111110 with p = 0.0009765625.\n",
      "Measured 10001000 with p = 0.001953125.\n",
      "Measured 10101000 with p = 0.001953125.\n",
      "Measured 11101000 with p = 0.00390625.\n",
      "Measured 10011000 with p = 0.0048828125.\n",
      "Measured 10111000 with p = 0.001953125.\n",
      "Measured 11011000 with p = 0.001953125.\n",
      "Measured 10001010 with p = 0.0029296875.\n",
      "Measured 10101010 with p = 0.001953125.\n",
      "Measured 11101010 with p = 0.001953125.\n",
      "Measured 10011010 with p = 0.0009765625.\n",
      "Measured 10111010 with p = 0.001953125.\n",
      "Measured 11011010 with p = 0.001953125.\n",
      "Measured 11111010 with p = 0.001953125.\n",
      "Measured 10001100 with p = 0.0029296875.\n",
      "Measured 10101100 with p = 0.00390625.\n",
      "Measured 11001100 with p = 0.0009765625.\n",
      "Measured 11101100 with p = 0.0009765625.\n",
      "Measured 10011100 with p = 0.0029296875.\n",
      "Measured 10111100 with p = 0.001953125.\n",
      "Measured 10001110 with p = 0.005859375.\n",
      "Measured 10101110 with p = 0.001953125.\n",
      "Measured 11001110 with p = 0.0029296875.\n",
      "Measured 11101110 with p = 0.001953125.\n",
      "Measured 10011110 with p = 0.0029296875.\n",
      "Measured 10111110 with p = 0.001953125.\n",
      "Measured 11011110 with p = 0.001953125.\n",
      "Measured 11111110 with p = 0.0029296875.\n",
      "Measured 00001001 with p = 0.001953125.\n",
      "Measured 00101001 with p = 0.0029296875.\n",
      "Measured 01001001 with p = 0.0029296875.\n",
      "Measured 01101001 with p = 0.0048828125.\n",
      "Measured 00011001 with p = 0.0048828125.\n",
      "Measured 01011001 with p = 0.0009765625.\n",
      "Measured 01111001 with p = 0.00390625.\n",
      "Measured 00001011 with p = 0.001953125.\n",
      "Measured 00101011 with p = 0.0029296875.\n",
      "Measured 01001011 with p = 0.001953125.\n",
      "Measured 00011011 with p = 0.0009765625.\n",
      "Measured 01111011 with p = 0.0009765625.\n",
      "Measured 00001101 with p = 0.0009765625.\n",
      "Measured 01001101 with p = 0.0009765625.\n",
      "Measured 00011101 with p = 0.0009765625.\n",
      "Measured 01011101 with p = 0.0029296875.\n",
      "Measured 00001111 with p = 0.0009765625.\n",
      "Measured 00101111 with p = 0.0029296875.\n",
      "Measured 01001111 with p = 0.0009765625.\n",
      "Measured 00011111 with p = 0.001953125.\n",
      "Measured 00111111 with p = 0.0009765625.\n",
      "Measured 01111111 with p = 0.0009765625.\n",
      "Measured 10001001 with p = 0.0029296875.\n",
      "Measured 10101001 with p = 0.00390625.\n",
      "Measured 11001001 with p = 0.001953125.\n",
      "Measured 11101001 with p = 0.0068359375.\n",
      "Measured 10011001 with p = 0.001953125.\n",
      "Measured 10111001 with p = 0.0029296875.\n",
      "Measured 11011001 with p = 0.00390625.\n",
      "Measured 11111001 with p = 0.0068359375.\n",
      "Measured 10001011 with p = 0.0048828125.\n",
      "Measured 10101011 with p = 0.00390625.\n",
      "Measured 11001011 with p = 0.00390625.\n",
      "Measured 11101011 with p = 0.0029296875.\n",
      "Measured 10011011 with p = 0.0009765625.\n",
      "Measured 10111011 with p = 0.001953125.\n",
      "Measured 11011011 with p = 0.001953125.\n",
      "Measured 11111011 with p = 0.001953125.\n",
      "Measured 10001101 with p = 0.0009765625.\n",
      "Measured 10101101 with p = 0.00390625.\n",
      "Measured 11101101 with p = 0.0029296875.\n",
      "Measured 10011101 with p = 0.00390625.\n",
      "Measured 10111101 with p = 0.0078125.\n",
      "Measured 11011101 with p = 0.005859375.\n",
      "Measured 11111101 with p = 0.0048828125.\n",
      "Measured 10001111 with p = 0.0009765625.\n",
      "Measured 10101111 with p = 0.001953125.\n",
      "Measured 11001111 with p = 0.001953125.\n",
      "Measured 11101111 with p = 0.0078125.\n",
      "Measured 10011111 with p = 0.0009765625.\n",
      "Measured 10111111 with p = 0.005859375.\n",
      "Measured 11011111 with p = 0.00390625.\n",
      "Measured 11111111 with p = 0.0009765625.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0, 0, 1, 0, 0, 0, 1, 0]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "run_entangle(eng, num_qubits=8)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
